/***************************************************************************
 *                                                                         *
 * Copyright (c) 2007 - 2009 Nuvoton Technology Corp. All rights reserved.*
 *                                                                         *
 ***************************************************************************/
 

#ifdef ECOS
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "drv_api.h"
#include "diag.h"
#include "wbio.h"
#include "NUC900_GE.h"
#include "NUC900_reg.h"
#else
#include <stdio.h>
#include "NUC900_GE.h"
#include "wbio.h"
#ifdef _NUC900_
#include "NUC900_reg.h"
#else
#include "reg702.h"
#endif
#endif

#include "fontdata.h"

int _font_id = F8x8;   // default
//int _font_id = F8x16;  // smf

void set_font_size(int size)
{
	_font_id = size;
}

void put_char(int x, int y, char asc_code, int fore_color, int back_color, int draw_mode)
{
  int cmd32, dest_pitch, src_pitch, pitch, dest_start, dest_dimension;
  UINT32 width, height;
  UINT32 fore_color32, back_color32;
  UINT8 *fptr;
  UINT8 *temp_buf[32*32], *ptr8;
  UINT16 *ptr16;
  int idx;
  int x1, y1, x2, y2;
  
  fore_color32 = make_color(fore_color);
  back_color32 = make_color(back_color);

  cmd32 = 0xcc430080; 
  
  //cmd32 = cmd32 | 0x400;   //i had not saw the destination update if setting this, smf
  
  if (draw_mode==MODE_TRANSPARENT)
  {
    cmd32 |= 0x00004000; // mono transparency
  }

  //cmd32 |= 0x00000200; // inside clip //smf, why to do that ??
  
  outpw(REG_2D_GEC, cmd32);

  outpw(REG_2D_GEFC, fore_color32);
  outpw(REG_2D_GEBC, back_color32);
  
  if (_font_id==F8x16)
  {
    fptr = (UINT8 *)&FontData16[asc_code][0];
    src_pitch = 8;
    width = 8;
    height = 16;
    
    ptr8 = (UINT8 *)&temp_buf[0];
    for (idx=0; idx<16; idx++)
    {
      *ptr8++ = *fptr++;
      *ptr8++ = 0;
      *ptr8++ = 0;
      *ptr8++ = 0;
    }
    fptr =  (UINT8 *)&temp_buf[0];
  }
  else /* F8x8 */
  {
    fptr = (UINT8 *)&FontData8[asc_code][0];

    x1 = x;
    y1 = y;
    x2 = x+8;
    y2 = y+8;
    
    //src_pitch = 32; 
    //width = 32;
    src_pitch = 8; // smf
    width = 8;  // smf
    height = 8;
    ptr8 = (UINT8 *)&temp_buf[0];
    for (idx=0; idx<8; idx++)
    {
      *ptr8++ = *fptr++;
      *ptr8++ = 0;
      *ptr8++ = 0;
      *ptr8++ = 0;
    }
    fptr =  (UINT8 *)&temp_buf[0];
  }

  dest_pitch = GFX_WIDTH; // pitch in pixels
  
  pitch = (dest_pitch << 16) | src_pitch;
  outpw(REG_2D_GESDP, pitch);  

  outpw(REG_2D_GEXYSORG, (int)fptr);
  outpw(REG_2D_GESSXYL, 0); // always start from (0,0)    
  
  dest_start = y << 16 | x;
  outpw(REG_2D_GEDSXYL, dest_start);
  
  dest_dimension = height << 16 | width;
  outpw(REG_2D_GEDIXYL, dest_dimension);  

  //outpw(REG_2D_GECBTL, ((y1 << 16) | x1));
  //outpw(REG_2D_GECBBR, ((y2 << 16) | x2));
    
  outpw(REG_2D_GETG, 1); 
  
  while ((inpw(REG_2D_GEINTS)&0x01)==0); // wait for command complete  

  outpw(REG_2D_GEINTS, 1); // clear interrupt status
}


void put_string(int x, int y, char *str, int fore_color, int back_color, int draw_mode)
{
  char *ptr;  /* point to string */
  int idx, char_width, sx;
  
  ptr =	str; 
  
  if (_font_id==F8x16)
  {
    char_width = 8;
  }
  else /* F8x8 */
  {
    char_width = 8;
  }
  
  sx = x;
  
  for (idx=0; idx<strlen(str); idx++)
  {
    put_char(sx, y, *ptr++, fore_color, back_color, draw_mode);
    sx += char_width;
  }
  
}


